#!/usr/bin/python
#coding: utf-8
#simple advEngine | version:0.201 修改了一个选项bug和选项判断语句，减少了代码行数 | winkidney littlekey

import pygame
from pygame.locals import *
from sys import exit
from time import sleep

import img
import musicfx
import text
from scripter import Scripter
from scripter import init_set_reader
import ui

import define


INIT_SET_FILE = "init_set.ini" #初始化游戏设置 的脚本


def create_game_menu(scripter):
    pass
        
        
def show_menu():
    pass    
    
def run():
    #初始化工作
    screen = pygame.display.set_mode(define.SCREEN_SIZE,define.SCREEN_FLAGS,define.SCREEN_DEPTH)
    pygame.display.set_caption(define.GAME_CAPTION)# set title
    pygame.init()# init pygame 
    clock = pygame.time.Clock()
    #脚本分析引擎初始化
    scripter = Scripter(define.SCRIPT_FILE,screen,define.SCREEN_SIZE)        
    #脚本引擎初始化结束
    #事件部分初始化
    pygame.event.set_allowed((MOUSEBUTTONDOWN,QUIT))
    #事件部分初始化完成
    #初始化完成，开始加载标题界面（主菜单
    scripter.bgm.play(define.START_BGM)
    scripter.background.load(define.START_BACKGROUND)
    scripter.text_object.get_text(u'')
    start_menu = ui.StartMenu(scripter,screen)
    start_menu.load()
    scripter.START_MENU_ON = True
    scripter.MENU_ON = False
    #界面加载完成
    while 1:
        left_button_pressed = False
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed()[0]:
                    left_button_pressed = True
                    #菜单未打开的时候的处理
                    if not (scripter.MENU_ON or scripter.START_MENU_ON or scripter.choosing_menu_on):
                        scripter.analysis(scripter.next_line())
                    #菜单打开后的左键单击处理                        
                if pygame.mouse.get_pressed()[2]:
                    print "rightclick"
                    if not scripter.MENU_ON:# what's this ? howto use ?
                        pass#scripter.MENU_ON = True
        clock.tick(30)
        scripter.background.show() # load file(background)  yet ?
        if scripter.character.image != None: scripter.character.show()
        if scripter.text_object.text_content and  not scripter.choosing_menu_on: scripter.text_object.text_display() # where is the 'text_content'
        if scripter.choosing_menu_on:
            choosing_menu_list = scripter.choosing_menu.show_and_test(define.CHOOSING_MENU_POINT[0],define.CHOOSING_MENU_POINT[1],pygame.mouse.get_pos(),left_button_pressed)
            #print choosing_menu_list
            for apple in xrange(len(choosing_menu_list)):
                if apple in choosing_menu_list:
                    scripter.file.seek(scripter.in_jumplist(scripter.potint_to_select[apple]),0)
                    scripter.potints_to_select = []
                    scripter.choosing_menu_on = False
                    scripter.analysis(scripter.next_line()) #4.14解决会显示上一次的文本的问题，在选项结束后立即调用一次下一行解析功能
            
        
        #全局开关MENU_ON，如果开关为真，则绘制主菜单并使用主菜单功能，如果为假则不绘制菜单
        if scripter.START_MENU_ON:
            main_menu_command_list = start_menu.show(define.MAIN_MENU_START_POINT[0],define.MAIN_MENU_START_POINT[1],pygame.mouse.get_pos(),left_button_pressed)
            #print main_menu_command_list
            if 'start' in main_menu_command_list:
                scripter.START_MENU_ON = False
            elif 'exit' in main_menu_command_list:
                exit()
        #if scripter.choosing_menu_on:
        

            
        if scripter.MENU_ON:
            pass
        pygame.display.update()
        
if __name__ == "__main__":
    init_set_reader(INIT_SET_FILE)
    run()
    
